package cn.edu.jxau.test;

import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.concurrent.ExecutionException;

public class Main {

	public static void main(String[] args) throws InterruptedException, ExecutionException {

		int[][] matrix = new int[][] { { 1, 2, 3, 4, 5 }, { 5, 6, 7, 8, 10 }, { 9, 10, 11, 12, 13 },
				{ 13, 14, 15, 16, 17 } ,{17,18,19,20,21}};
		print(matrix);
	}

	public static void print(int[][] matrix) {

		// 参数校验 //
		Objects.requireNonNull(matrix, "参数matrix不能为null");
		int len = matrix[0].length;
		if (len == 0) { // 空矩阵
			return;
		}
		for (int i = 1; i < matrix.length; i++) {
			if (matrix[i].length != len) {
				throw new IllegalArgumentException("参数matrix不是矩阵");
			}
		}

		print(matrix, 0, 0, matrix.length, matrix[0].length);
	}

	public static void print(int[][] matrix, int i, int j, int rows, int columns) {

		if (rows <= 0 || columns <= 0) {
			return;
		}
		// 从左至右打印第一行 //
		for (int k = 0; k < columns; k++, j++) {
			System.out.print(matrix[i][j] + "\t");
		}
		j--;

		// 从上至下打印最后一列 //
		i++;
		for (int k = 0; k < rows - 1; k++, i++) {
			System.out.print(matrix[i][j] + "\t");
		}
		i--;

		// 从右至左打印最后一行 //
		j--;
		for (int k = 0; k < columns - 1; k++, j--) {
			System.out.print(matrix[i][j] + "\t");
		}
		j++;

		// 从下至上打印第一列 //
		i--;
		for (int k = 0; k < rows - 2; k++, i--) {
			System.out.print(matrix[i][j] + "\t");
		}

		print(matrix, i + 1, i + 1, rows - 2, columns - 2);
	}
}